#include "util.h"
int util_get_char()
{

    struct termios oldt, newt;
    int input = -1;
    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO); // 新的属性改为不现实echo和立即返回
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);

    // 设置 stdin 非阻塞
    int oldf;
    oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
    fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

    input = getchar();
    if (input == 27)
    {
        int second = getchar();
        int third = getchar();
        if (second == 91)
        {
            switch (third)
            {
            case 'A':
                input = 1000;
                break;
            case 'B':
                input = 1001;
                break;
            case 'C':
                input = 1002;
                break;
            case 'D':
                input = 1003;
                break;
            case 51:
                if (getchar() == 126)
                    input = 2001;

                break;
            default:
                break;
            }
        }
    }
    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    fcntl(STDIN_FILENO, F_SETFL, oldf);
    return input;
}
int util_clamp(int a, int min, int max)
{
    if (min > max)
        printf("util clamp error !!!\n");
    if (a >= max)
        a = max - 1;
    if (a < min)
        a = min;
    return a;
}